package com.xframe.xdal.core.model.sqlbuilder.proc;

import com.xframe.xdal.core.model.mapping.ITableInfo;
import com.xframe.xdal.core.model.sqlbuilder.BaseSqlBuilder;
import com.xframe.xdal.core.model.sqlbuilder.BuilderContext;
import com.xframe.xdal.core.model.sqlbuilder.SqlExpression;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;


/**
 * 功能描述：自由的 构造更新sql语句
 * 创建人：xufeng
 * 创建时间：2022/10/8 10:04
 *
 * @version 1.0
 **/
@Slf4j
public class DefUpdateManyBuilder extends BaseSqlBuilder {

    /**
     * 更新sql 语句
     *
     * @return sql 语句
     */
    public String getSQL() {
        return " update %s set %s where 1=1 %s ";
    }

    @Override
    public SqlExpression build(BuilderContext context) {

        List<Object> pList = new ArrayList<>();
        StringBuilder setBuilder = new StringBuilder();
        StringBuilder whereBuilder = new StringBuilder();
        ITableInfo tableInfo = context.getTableInfo();
        context.getSetWrapCollection().buildSet(context,tableInfo,setBuilder,pList);
        // 处理查询条件
        context.getConditionValueCollection().builderWhereByKeyNoQuery(context,tableInfo,context.getColPlaceholder(),whereBuilder,pList);
        String updateSql = String.format(getSQL(), context.getColPlaceholder()
                + context.getNameConvert().toName(tableInfo.getName()) + context.getColPlaceholder(), setBuilder, whereBuilder);
        return new SqlExpression(updateSql, pList,context.getDataSourceTypeEnums());
    }
}
